import config from '../../utils/config'
import {request} from "../../utils/request";

const app = getApp()
Page({
    data: {
        picUrl: config.picUrl,
        custom: wx.getMenuButtonBoundingClientRect(),
        user: {},
        status: '',
        page: 1,
        last_page: 1,
        list: [],
        endHide:false
    },
    onLoad(options) {
        var user = wx.getStorageSync('userInfo')
        this.setData({user})
        this.getList()
    },
    getList() {
        request({
            url: "/miniprogram/order/getMyOrder",
            data: {page: this.data.page, status: this.data.status, user_id: this.data.user.id},
            type: 'post',
        }).then(res => {
            if (this.data.page == 1) this.setData({list: []})
            this.setData({
                list: this.data.list.concat(res.data),
                last_page: res.last_page
            })
        })
    },
    cancelOrder(e) {
        request({
            url: "/miniprogram/order/handleCancelOrder",
            data: {id: e.currentTarget.dataset.id}
        }).then(res => {
            config.toast(res.msg, res.code, () => {
                if (res.code == 200) {
                    var list = this.data.list
                    list[e.currentTarget.dataset.index].status = -1
                    this.setData({list})
                }
            })
        })
    },
    confirmOrder(e) {
        wx.showModal({
          title: '提示',
          content: '请确认您已收到货物并确认产品无误，确认后无法再进行退货操作。',
          success: (res) => {
            if (res.confirm) {
                wx.showLoading()
                request({
                    url: "/miniprogram/lklpay/handleConfirmOrder",
                    data: {id: e.currentTarget.dataset.id}
                }).then(res => {
                    wx.hideLoading()
                    config.toast(res.msg, res.code, () => {
                        if (res.code == 200) {
                            var list = this.data.list
                            list[e.currentTarget.dataset.index].status = 3
                            this.setData({list})
                        }
                    })
                })
            }
          }
        })
        
    },
    rePayOrder(e) {
        var _this = this,
            order = this.data.list[e.currentTarget.dataset.index]
        request({
            url: "/miniprogram/lklpay/reCreateOrder",
            data: {
                openid: this.data.user.openid,
                id: e.currentTarget.dataset.id,
                user_id: this.data.user.id,
                nickname: this.data.user.nickname
            },
            type: 'post'
        }).then(res => {
            if(res.code !== 200){
                config.toast(res.msg,201,()=>{})
                return false
            }
            wx.showLoading()
            wx.requestPayment({
                timeStamp: res.data.acc_resp_fields.time_stamp,
                nonceStr: res.data.acc_resp_fields.nonce_str,
                package: res.data.acc_resp_fields.package,
                signType: res.data.acc_resp_fields.sign_type,
                paySign: res.data.acc_resp_fields.pay_sign,
                success(rel) {
                    setTimeout(() => {
                        request({
                            url: "/miniprogram/lklpay/queryOrder",
                            data: {out_trade_no: res.data.out_trade_no}
                        }).then(res2 => {
                            config.toast(res2.msg, res2.code, () => {
                                if (res2.code == 200) {
                                    var list = _this.data.list
                                    list[e.currentTarget.dataset.index].status = 1
                                    _this.setData({list})
                                }
                            })
                        })
                    }, 1500)
                },
                fail(err) {
                    wx.hideLoading()
                }
            })
        })
    },
    changeStatus(e){
        var status = e.currentTarget.dataset.status
        this.setData({
            status,
            page:1,
            last_page:1,
            endHide:true
        })
        this.getList()
    },
    onReachBottom() {
        if(this.data.page >= this.data.last_page){
            this.setData({endHide:false})
            return false;
        }
        this.setData({page:this.data.page+1})
        this.getList()
    }
})